package com.cykj.page03.servlet;

import com.alibaba.fastjson2.JSON;
import com.cykj.page03.MyHttpRequest;
import com.cykj.page03.MyHttpResponse;
import com.cykj.page03.dto.ResponseDTO;
import com.cykj.page03.service.impl.UserServiceImpl;

/**
 * @BelongsProject: HandTomcat
 * @BelongsPackage: com.cykj.page03.servlet
 * @Author: WWJ
 * @CreateTime: 2025-06-19 09:49
 * @Description: TODO
 * @Version: 1.0
 */
public class EnrollServlet extends BaseServlet {


    @Override
    public void doGet(MyHttpRequest httpRequest, MyHttpResponse httpResponse) {
        doPost(httpRequest, httpResponse);
    }

    @Override
    public void doPost(MyHttpRequest httpRequest, MyHttpResponse httpResponse) {
        String acc = httpRequest.getRequestParamToKey("account");
        String pwd = httpRequest.getRequestParamToKey("password");
        String name = httpRequest.getRequestParamToKey("name");

        // 创建用户服务的实现类对象，用于处理注册逻辑
        UserServiceImpl userService = new UserServiceImpl();
        // 调用用户注册方法，将账号、密码、姓名传入，返回封装好的响应数据
        ResponseDTO dto = userService.enroll(acc, pwd, name);
        // 将返回的数据对象转换为 JSON 字节数组并写入响应体，返回给前端
        httpResponse.write(JSON.toJSONBytes(dto));
    }
}
